
"use strict";

var website = window.location.origin;
var hostName = window.location.hostname;

var heartinterval;
var openid, nickName;
var headnum = 1;
var avatarUrl = website + '/addons/' + moduleName + '/public/images/user/user' + headnum + '.png';
var userinfo;
var tomanager = [];
var messagePrivate = [];
var newMessage = [];
var saytowho = '';
var openidnow = '';
var isalluser = true;
var canRefresh = true;
var room_id = 'room_1';
var room_idOld;
var room_name = '聊天室';
var newAudio;
var emojis = [];
var usernotice = true;
var emojiChar = "😀-😋-😌-😍-😎-😅-😏-😐-😑-😧-😓-😛-😜-😝-😠-😡-😢-😣-😤-😢-😦-😯-😨-😩-😪-😫-😬-😭-😰-😱-✊-✋-✌️-👆-👇-👈-👉-👊-👋-👌-👍-👎-👏-🙏";
var emoji = [
	"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17",
	"18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30",
	"31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44"];



$(function () {



	(function () {
		$.ajax({
			url: we7url + 'getSessionLogin',
			type: 'GET',
			success: function (res) {
				if (res) {

					userinfo = res;
					openid = res.openid;
					openidnow = openid;
					avatarUrl = res.avatarUrl;
					nickName = res.nickName;
					room_id = res.room_id;
					room_idOld = room_id;
					room_name = res.roomName;

					checkin();
					getAllMember();

				} else {
					/*登录界面函数*/
					$('.login-wrap').show();
					login();
				}
			}
		})
	})();





	window.addEventListener('pagehide', function (event) {
		if (isWeiXin()) {
			$.ajax({
				url: we7url + 'clearSession',
				success(res) {
					if (res == "OK") {

					}

				}
			});
		}
	}


	);



	document.getElementById('emoji').addEventListener('click', function (e) {

		var emojiwrapper = document.getElementById('emojiWrapper');
		if (emojiwrapper.style.display != 'block') {
			emojiwrapper.style.display = 'block';
		} else {
			emojiwrapper.style.display = 'none';
		}
		e.stopPropagation();
	}, false);


	document.getElementById('notice').addEventListener('click', function (e) {

		var notice = document.getElementById('notice');
		if (usernotice) {
			usernotice = false;
			notice.className = 'fa fa-bell-slash-o';
		} else {
			usernotice = true;
			notice.className = 'fa fa-bell-o';
		}
		e.stopPropagation();
	}, false);


	document.getElementById('member').addEventListener('click', function (e) {

		var memberdiv = document.getElementsByClassName('member-con')[0];
		if (memberdiv.style.display == "none") {
			memberdiv.style.display = "block";
		} else {
			memberdiv.style.display = "none";
		}
		e.stopPropagation();
	}, false);


	document.body.addEventListener('click', function (e) {
		if ($('#chat-wrap').css("display") === "none") {
			var headportrait = document.getElementById('headportrait');
			if (e.target != headportrait) {
				headportrait.style.display = 'none';
			};
		}
		else {
			var emojiwrapper = document.getElementById('emojiWrapper');
			if (e.target != emojiwrapper) {
				emojiwrapper.style.display = 'none';
			};
			var memberdiv = document.getElementsByClassName('member-con')[0];
			if (e.target != memberdiv) {
				memberdiv.style.display = 'none';
			};
		}
	});






	$("#nickNameLogin").click(function () {
		var loginName = $.trim($("#loginName").val());
		if (loginName) {
			var loginInfo = { 'nickName': loginName };
			$.ajax({
				url: we7url + 'nickNameLogin',
				type: 'POST',
				data: loginInfo,
				success(res) {
					if (res == 'OK') {

						var reg = new RegExp('\\?', '');
						if (reg.test(location.href)) {
							window.location.href = we7url + 'index&times=' + (new Date()).getTime();
						} else {
							window.location.href = we7url + 'index&times=' + (new Date()).getTime();
						}


					} else {
						alert("填写信息有误！");
					}
				}
			});
		} else {
			alert("填写信息有误！");
		}

	});




	/*发送消息*/
	$('.sendBtn').click(function () {
		sendMessage();
	});


	document.onkeydown = function () {
		if (window.event.keyCode == 13) {
			if ($('#showchatroom').css('display') === "block") {
				$('.sendBtn').click();
			}
		}

	};

	document.getElementById('emojiWrapper').addEventListener('click', function (e) {

		var target = e.target;

		if (target.nodeName.toLowerCase() == 'img') {
			var sendtxt = document.getElementById('sendtxt');
			sendtxt.focus();


			var emojiNow = "";
			for (var val of emojis) {
				if (val.emoji == target.num) {
					emojiNow = val.char;
				}
			}
			if (emojiNow) {
				sendtxt.value = sendtxt.value + emojiNow;
			} else {
				sendtxt.value = sendtxt.value + '[emoji:' + target.num + ']';
			}
			var emojiwrapper = document.getElementById('emojiWrapper');
			emojiwrapper.style.display = 'none';
		};
		e.stopPropagation();
	}, false);

	document.getElementById('sendImage').addEventListener('change', function () {

		if (this.files.length != 0) {

			for (let i = 0; i < this.files.length; i++) {
				var file = this.files[i];
				uploadImage(file);
			};
		};
	}, false);



});





function uploadImage(file) {

	if (!file) {
		return;
	}
	new Compressor(file, {
		maxWidth: 600,
		quality: 0.6,
		success: function (result) {

			var formdata = new FormData();
			formdata.append('upimg', result);

			$.ajax({
				url: we7url + 'sendimg',
				data: formdata,
				type: "POST",
				catch: false,
				contentType: false,
				processData: false,
				success: function (result) {
					if (result != '') {


						var imgurl = website + '/addons/' + moduleName + '/public/image/' + result + '';
						if (room_id == 'room_private') {
							var imgmsg = {
								msg: imgurl,
								openid: openid,
								sendtype: 'img',
								avatarUrl: avatarUrl,
								nickName: nickName,
								saytoone: openidnow,
								roomid: 'room_private'
							};
						} else {
							var imgmsg = {
								msg: imgurl,
								openid: openid,
								sendtype: 'img',
								avatarUrl: avatarUrl,
								nickName: nickName,
							};
						}
						sendAjax(imgmsg);
					}
				}
			});
		}
	});
}



/*主函数已经没用了*/
function main() {

	$('.chat-wrap').hide();
	/*键盘回车事件*/
	keydownOld();

	/*如果点击的不是头像界面或表情界面，则将其隐藏*/



	window.onscroll = function () {

		let top = window.pageYOffset || document.documentElement.scrollTop;
		if (isNewInWindow() || top === 0) {

			document.getElementById('toNewMessage').style.display = "none";
		}
		else {

			document.getElementById('toNewMessage').style.display = "inline";
		}

		var toNewMessage = document.getElementById("toNewMessage");
		toNewMessage.onclick = function () {
			scrollToEnd();
		}
	}

}

/*登录界面函数*/
function login() {

	/*键盘回车事件*/
	keydownOld();


}


/*键盘回车事件*/
function keydownOld() {

	$(document).keydown(function (event) {
		/*回车事件*/
		if (event.keyCode == 13 && $('#chat-wrap').css("display") === "none") {
			nickName = $.trim($('#loginName').val());
			if (nickName) {



				main();
			} else {
				alert('请输入昵称');
			}
		}
		else if (event.keyCode == 13 && $('#chat-wrap').css("display") !== "none") {
			sendMessage();
		}
	})
}

/*隐藏登录界面 显示聊天界面*/
function checkin(data) {
	var emojiContainer = document.getElementById('emojiWrapper'),
		docFragment = document.createDocumentFragment();


	for (let i = 1; i <= 44; i++) {

		var emojiItem = document.createElement('img');
		emojiItem.src = moduleUrl + 'public/images/emoji/' + i + '.png';
		emojiItem.title = i;
		emojiItem.num = i;

		docFragment.appendChild(emojiItem);

	};
	emojiContainer.appendChild(docFragment);


	var em = {},
		emChar = emojiChar.split("-");
	emoji.forEach(function (v, i) {
		em = {
			char: emChar[i],
			emoji: v
		};
		emojis.push(em)
	});
	getmessage(20);
	$('.chat-wrap').show();

}


function getAllMember() {
	$.ajax({
		url: we7url + 'getAllMember',
		data: {},
		type: "GET",
		success: function (res) {
			if (res && res.length > 0) {
				$(".member-con").empty();
				var data = res;
				var onlineHtml = '';
				for (let i = 0; i < data.length; i++) {

					if (data[i].manager == 1) {
						onlineHtml += '<div class="onlinediv" style="order:1;"><img class="onlineimg" src="' + data[i].avatarUrl + '"/><span class="onlinespanname">' + data[i].nickName + '</span><span style="display:none;" class="onlinemanage">管理员</span></div>';
					} else if (data[i].manager == 0) {
						onlineHtml += '<div class="onlinediv"  style="order:10;"><img class="onlineimg" src="' + data[i].avatarUrl + '"/><span class="onlinespanname">' + data[i].nickName + '</span><span style="display:none;" class="onlinemanage">用户</span></div>';
					} else {
						onlineHtml += '<div class="onlinediv"  style="order:100;"><img class="onlineimg" src="' + data[i].avatarUrl + '"/><span class="onlinespanname">' + data[i].nickName + '</span><span style="display:none;" class="onlinemanage">访客</span></div>';
					}

				}

				$(".member-con").append(onlineHtml);

			}
		}
	});
}


var pdr_time = 0;
var msgsTopId_now = false;
function pullDownRefresh(msgmuch, firstLoad = false) {
	if ($(".chat-con").children('.chat-item').length <= 0) return;

	var topnow = $(".chat-con")[0].scrollTop;
	if (((topnow <= 20 && topnow >= 0) || firstLoad) && canRefresh) {
		var pdr_time_now = new Date().getTime();
		if (pdr_time_now - pdr_time < 1000) {
			var otop = $(".chat-con").find("span[data-midnow=" + msgsTopId_now + "]").parent("div")[0].offsetTop;
			$(".chat-con").scrollTop(otop);
			return;
		}
		pdr_time = pdr_time_now;
		canRefresh = false;
		$("#loadingDiv").show();

		var msgsTopId = parseInt($(".chat-con").children('.chat-item').first().children("span[data-midnow]")[0].dataset.midnow);
		if (!msgsTopId && room_id == "room_private") {
			var msgTopDiv = $(".chat-con").children('div.chat-item:nth-child(2)').children("span[data-midnow]");
			if (msgTopDiv.length <= 0) {
				canRefresh = true;
				$("#loadingDiv").hide();
				return;
			}
			msgsTopId = parseInt(msgTopDiv[0].dataset.midnow);
		}
		if (!msgsTopId) {
			canRefresh = true;
			$("#loadingDiv").hide();
			return;
		} else {
			msgsTopId_now = msgsTopId;

			$.ajax({
				url: we7url + 'pullDownRefresh',
				data: { msgsTopId: msgsTopId, roomIdNow: room_id, much: msgmuch },
				type: "GET",
				success: function (res) {

					if (res && typeof res == "object" && res.length > 0) {

						if (room_id == 'room_private') {

						} else {


							var data = res;
							for (let i = data.length - 1; i >= 0; i--) {
								switch (data[i].sendtype) {
									case "text":
										showMessage(data[i], false);
										break;
									case "img":
										showImage(data[i], false);
										break;
								}

							}

						}

						var otop = $(".chat-con").find("span[data-midnow=" + msgsTopId + "]").parent("div")[0].offsetTop;
						$(".chat-con").scrollTop(otop);

					}
				},
				complete(res) {
					canRefresh = true;
					$("#loadingDiv").hide();

				}
			});
		}

	}
}


function newGuid() {
	var guid = "";
	for (var i = 1; i <= 32; i++) {
		var n = Math.floor(Math.random() * 16.0).toString(16);
		guid += n;
		if ((i == 8) || (i == 12) || (i == 16) || (i == 20))
			guid += "-";
	}
	return guid;
}

function sendAjax(data) {
	$.ajax({
		url: we7url + "sendAjax",
		data: data,
		type: "POST",
		timeout: 60000,
		success(res) {

		}
	});
}



function getAjax(msgtime = 0) {
	$.ajax({
		url: we7url + "getAjax",
		type: "POST",
		data: { msgtime: msgtime },
		timeout: 60000,
		success(response) {

			if (response.status == 1) {
				if (response.response_type == 'is_read') {
					var data = response.info;
					for (let i = 0; i < data.length; i++) {
						switch (data[i].sendtype) {
							case "text":
								showMessage(data[i]);
								break;
							case "img":
								showImage(data[i]);
								break;
						}

					}
					if (isshownotice && usernotice) {
						var notice = moduleUrl + "public/notice/notice.mp3";
						newAudio = new Audio(notice);
						newAudio.play();
					}

					getAjax(response.timenow);

				}
				if (response.response_type == 'is_connecting') {
					getAjax(msgtime);
				}
			} else {
				getAjax(msgtime);
			}
		},
		error(res) {
			getAjax(msgtime);
		}
	});
}



function getmessage(t) {
	$.ajax({
		url: we7url + 'getmessage',
		data: { msgtimes: 0, much: t, room_id: room_id },
		type: "GET",
		success: function (res) {
			if (res && res.length > 0) {

				var data = res;
				for (let i = 0; i < data.length; i++) {
					switch (data[i].sendtype) {
						case "text":
							showMessage(data[i]);
							break;
						case "img":
							showImage(data[i]);
							break;
					}

				}

				/* 开始长连接轮询 */
				getAjax(data[data.length - 1].sendtime);

			} else {
				getAjax(0);
			}
		}
	});

}

function isEmoji(msg) {
	var result = msg;
	var reg = /\[emoji:\d+\]/g;
	var result = reg.test(msg);
	return result;
}

/*分析文字并用表情包替换emoji*/
function showEmoji(msg) {
	var match, result = msg,
		reg = /\[emoji:\d+\]/g,
		emojiIndex,
		totalEmojiNum = document.getElementById('emojiWrapper').children.length;
	while (match = reg.exec(msg)) {
		emojiIndex = match[0].slice(7, -1);
		if (emojiIndex > totalEmojiNum) {
			result = result.replace(match[0], '[X]');
		} else {
			result = result.replace(match[0], '<img class="emojiImg" src="' + moduleUrl + 'public/images/emoji/' + emojiIndex + '.png" />');

		};
	};
	return result;
}

function showEmojiReal(arg) {
	var regRule = /\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]|\u270a|\u270b|\u270c\ufe0f/i;
	var match;
	if (regRule.test(arg)) {
		while (match = regRule.exec(arg)) {
			for (var v of emojis) {
				if (v['char'] == match[0]) {
					arg = arg.replace(match[0], "[emoji:" + v['emoji'] + "]");
				}

				if (v['emoji'] == '44' && v['char'] != match[0]) {
					arg = arg.replace(match[0], "");
				}
			}
		}

	}
	return arg;

}

function sendMessage() {


	var txt = filterXSS($('#sendtxt').val());
	txt = showEmojiReal(txt);
	$('#sendtxt').val('');
	if (txt) {

		if (room_id == 'room_private') {
			var wsmsg = { openid: openid, msg: txt, sendtype: 'text', avatarUrl: avatarUrl, nickName: nickName, saytoone: openidnow, saytowho: '', roomid: 'room_private' };
		} else {
			var wsmsg = { openid: openid, msg: txt, sendtype: 'text', avatarUrl: avatarUrl, nickName: nickName, saytowho: saytowho };
		}
		sendAjax(wsmsg);
	}
	saytowho = '';
	$('#saytowhoDiv').text('');
}

/*显示消息 '&nbsp;'+'&nbsp;'+'&nbsp;' + data.sendtime+*/
function showMessage(data, isAppend = true) {
	var html;
	var msg;


	if (isEmoji(data.msg)) {
		msg = showEmoji(data.msg);
		if (data.saytowho) {
			msg = showSaytowho(msg, data.openid, data.nickName, data.saytowho);
		}
		if (data.openid === openid) {
			html = '<div class="chat-item item-right clearfix rela"><span class="abs nickName" data-midnow=' + data.Id + '>' + data.nickName + '</span><span data-nickName=' + data.nickName + ' onclick="saytowhoFun(this)" class="imgavatar fr" style="background-image:url(' + data.avatarUrl + ')"></span><span class="fr message" data-msgid="' + data.Id + '" data-time="' + data.sendtime + '" data-openid="' + data.openid + '" onclick="copyback(this)">' + msg + '</span></div>'
		} else {
			html = '<div class="chat-item item-left clearfix rela"><span class="abs nickName" data-midnow=' + data.Id + '>' + data.nickName + '</span><span  data-nickName=' + data.nickName + ' onclick="saytowhoFun(this)" class="imgavatar fl" style="background-image:url(' + data.avatarUrl + ')"></span><span class="fl message" data-msgid="' + data.Id + '" data-time="' + data.sendtime + '" data-openid="' + data.openid + '"  onclick="copyback(this)">' + msg + '</span></div>'

		}

	} else {
		msg = data.msg;
		if (data.saytowho) {
			msg = showSaytowho(msg, data.openid, data.nickName, data.saytowho);
		}
		if (data.openid === openid) {
			html = '<div class="chat-item item-right clearfix rela"><span class="abs nickName" data-midnow=' + data.Id + '>' + data.nickName + '</span><span  data-nickName=' + data.nickName + ' onclick="saytowhoFun(this)" class="imgavatar fr" style="background-image:url(' + data.avatarUrl + ')"></span><span class="fr message" data-msgid="' + data.Id + '" data-time="' + data.sendtime + '" data-openid="' + data.openid + '" onclick="copyback(this)">' + msg + '</span></div>'
		} else {
			html = '<div class="chat-item item-left clearfix rela"><span class="abs nickName" data-midnow=' + data.Id + '>' + data.nickName + '</span><span  data-nickName=' + data.nickName + ' onclick="saytowhoFun(this)" class="imgavatar fl" style="background-image:url(' + data.avatarUrl + ')"></span><span class="fl message" data-msgid="' + data.Id + '"  data-time="' + data.sendtime + '" data-openid="' + data.openid + '" onclick="copyback(this)">' + msg + '</span></div>'
		}
	}




	if (isAppend) {
		$('.chat-con').append(html);
		showMsgTime(data);
		if (isNewInWindow() || (data.openid == openid)) {
			scrollToEnd();
		}
	} else {
		$('.chat-con').prepend(html);
		showMsgTime(data, false);
	}


}

function showMsgTime(data, isAppend = true) {

	if ($('.chat-con')[0].children.length % 7 == 0) {
		var htmltime = "<div class='msgtime' style='font-size:12px;'>" + data.sendtime + "</div>";
		if (isAppend) {
			$('.chat-con').append(htmltime);
		} else {
			$('.chat-con').prepend(htmltime);
		}
	}
}

function showSaytowho(msg, sendopenid, sendnickname, towho) {
	var msgtowho = '';
	if (userinfo.openid == sendopenid) {
		msgtowho = '<span style="color:red;">你@' + towho + ':&nbsp;</span>' + msg;
	} else if (userinfo.nickName == towho) {
		msgtowho = '<span style="color:red;">' + sendnickname + '@你:&nbsp;</span>' + msg;
	} else {
		msgtowho = '<span style="color:green;">' + sendnickname + '@' + towho + ':&nbsp;</span>' + msg;
	}
	return msgtowho;
}

function showWelcome(name) {
	var html;

	html = '<div style="color:red;font-size:12px;">欢迎' + name + '进入聊天室</div>';
	if (room_id == "room_private") return;
	setTimeout(function () {
		$('.chat-con').append(html);
		if (isNewInWindow()) {
			scrollToEnd();
		}
	}, 4000);

}

/*将页面下拉到最新消息处*/
function scrollToEnd() {

	var h = $(".chat-con")[0].scrollHeight;
	$(".chat-con").scrollTop(h);
}

/*判断当有新信息来时，用户是否在页面底端*/
function isNewInWindow() {




	var l = document.getElementsByClassName("chat-item");

	if (isInWindow(l[l.length - 1])) {
		return true;
	}
	return false;
}

/*判定元素是否在界面内*/
function isInWindow(x) {
	var h = $(".chat-con")[0].clientHeight;
	if (x.getBoundingClientRect().top > 2 * h) {

		return false;
	}
	else if (x.getBoundingClientRect().bottom < 0) {

		return false;
	}
	return true;
}


/*显示图片 */
function showImage(data, isAppend = true) {
	var msgToDisplay = document.createElement('p');
	msgToDisplay.style.color = '#000';
	var html;
	if (data.openid === openid) {
		html = '<div class="chat-item item-right clearfix rela"><span class="abs nickName" data-midnow=' + data.Id + '>' + data.nickName + '</span><span   data-nickName=' + data.nickName + ' onclick="saytowhoFun(this)" class="imgavatar fr" style="background-image:url(' + data.avatarUrl + ')"></span><img onclick="enlargeimg(this)" src="' + data.msg + '" style = "margin-top:20px; max-width: 200px;max-height: 200px;float: right"/></div>'

	} else {
		html = '<div class="chat-item item-left clearfix rela"><span class="abs nickName" data-midnow=' + data.Id + '>' + data.nickName + '</span><span  data-nickName=' + data.nickName + ' onclick="saytowhoFun(this)"  class="imgavatar fl" style="background-image:url(' + data.avatarUrl + ')"></span><img onclick="enlargeimg(this)" src="' + data.msg + '" style = "margin-top:20px; max-width: 200px;max-height: 200px;float: left"/></div>'

	}
	if (isAppend) {
		$('.chat-con').append(html);
		showMsgTime(data);
		if (isNewInWindow() || (data.openid == openid)) {
			scrollToEnd();
		}
	} else {
		$('.chat-con').prepend(html);
		showMsgTime(data, false);
	}
}

function getNowFormatDate() {
	var date = new Date();
	var seperator1 = "-";
	var seperator2 = ":";
	var month = date.getMonth() + 1;
	var strDate = date.getDate();
	if (month >= 1 && month <= 9) {
		month = "0" + month;
	}
	if (strDate >= 0 && strDate <= 9) {
		strDate = "0" + strDate;
	}
	var currentdate = month + seperator1 + strDate
		+ " " + date.getHours() + seperator2 + date.getMinutes()
		+ seperator2 + date.getSeconds();
	return currentdate;
}



function clickImageButton() {
	document.getElementById("sendImage").click();
}


function getUserinfo() {
	$.ajax({
		url: we7url + 'getSessionopenid',
		type: 'POST',
		success: function (res) {
			if (res != '' && res != 'roomcode') {
				userinfo = res;
			}
		}
	})
}

function getUserinfoSyncTwo() {
	$.ajax({
		url: we7url + 'getSessionopenid',
		type: 'POST',
		async: false,
		success: function (res) {
			if (res != '' && res != 'roomcode') {
				userinfo = res;
			}
		}
	})
}

function onerrorImg(e) {
	e.src = '/addons/' + moduleName + '/public/images/room_1.jpg';
	e.onerror = null;
}

function saytowhoFun(e) {
	var name = e.dataset.nickname;
	var textnow = $("#saytowhoDiv").text();
	if (userinfo.nickName == name) return;

	if (textnow == "@" + name + "") {
		var textnow = $("#saytowhoDiv").text('');
		$("#remarkDiv").text('');
		saytowho = '';

	} else {
		$("#saytowhoDiv").text("@" + name + "");
		saytowho = name;

	}

}


function inputat() {
	var i = $("#sendtxt").val();
	if (i == "@") {
		Atallmember();
	}
}

function atperson(e) {
	var uid = e.dataset.uid;
	var nick = e.dataset.nick;
	$("#sendtxt").val("@" + nick + "  ");
	changeTabchat();
}

function enlargeimg(e) {
	var imgsrc = e.src;
	$("#largeimg").attr("src", imgsrc);
	$("#largeimgdiv").show();
	$("#imgbg").show();
}

$("#largeimgdiv").click(function () {
	$("#largeimgdiv").hide();
	$("#imgbg").hide();
});
$("#largeimg").click(function (event) {

});

$("#largeimg").on('mousewheel', function (event) {

	if (event.deltaY > 0) {
		var w = $("#largeimg").css('width');
		$("#largeimg").css("width", parseInt(w) + 50);
	}
	if (event.deltaY < 0) {
		var w = $("#largeimg").css('width');
		$("#largeimg").css("width", parseInt(w) - 50);
	}
});


function copyback(e) {

	var id = e.dataset.msgid;
	$("#copyid").val(id);
	var oid = $("span[data-msgid=" + id + "]").attr("data-openid");


	var textNow = $(e).text().trim();
	var reg = /^((https|http|ftp|rtsp|mms)?:\/\/)[^\s]+/;
	var isWeb = reg.test(textNow);

	if (userinfo.manager != 1 && userinfo.openid != oid && !isWeb) {
		return;
	} else {

		if (userinfo.manager != 1 && userinfo.openid != oid) {
			$("#isRevoke").hide();
			$("#revokeLi").hide();
		} else {
			$("#isRevoke").show();
			$("#revokeLi").show();
		}

		if (isWeb) {
			$("#visitWeb").show();
			$("#visitLi").show();
			$("#visitUrl").attr("href", textNow);
		} else {
			$("#visitWeb").hide();
			$("#visitLi").hide();
		}
		$("#copyback").modal("toggle");
	}

}

function copy() {
	var msgid = $("#copyid").val();
	$("span[data-msgid=" + msgid + "]").attr("data-msg");

}



function textareaPaste() {
	var cbd = event.clipboardData;
	var ua = window.navigator.userAgent;


	if (!(event.clipboardData && event.clipboardData.items)) {
		return;
	}


	if (cbd.items && cbd.items.length === 2 && cbd.items[0].kind === "string" && cbd.items[1].kind === "file" &&
		cbd.types && cbd.types.length === 2 && cbd.types[0] === "text/plain" && cbd.types[1] === "Files" &&
		ua.match(/Macintosh/i) && Number(ua.match(/Chrome\/(\d{2})/i)[1]) < 49) {
		return;
	}

	for (var i = 0; i < cbd.items.length; i++) {
		var item = cbd.items[i];

		if (item.kind == "file" && item.type.match(/^image/)) {
			var blob = item.getAsFile();
			if (blob.size === 0) {
				return;
			} else {


				uploadImage(blob);
			}
		}
	}
}

$('input,textarea').on('blur', function () {
	setTimeout(function () {
		window.scroll(0, 0);
	}, 400);
});